package cn.com.anysdk.starter.config.oss;

import cn.com.anysdk.oss.api.IOssService;
import cn.com.anysdk.oss.config.TencentCloudOssConfig;
import cn.com.anysdk.oss.impl.MinioOssService;
import cn.com.anysdk.oss.impl.TencentCloudOssService;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(TencentCloudOssProperties.class)
@ConditionalOnProperty(prefix = "anysdk.oss.tencent", name = "enabled", havingValue = "true", matchIfMissing = true)
public class TencentCloudOssAutoConfiguration {

    @Bean
    public TencentCloudOssConfig tencentCloudOssConfig(TencentCloudOssProperties properties) {
        return TencentCloudOssConfig.builder()
                .endpoint(properties.getEndpoint())
                .accessKeyId(properties.getAccessKeyId())
                .accessKeySecret(properties.getAccessKeySecret())
                .bucketName(properties.getBucketName())
                .isPrivate(properties.isPrivate())
                .isIntranet(properties.isIntranet())
                .customDomain(properties.getCustomDomain())
                .build();
    }

    @Bean
    public COSClient cosClient (TencentCloudOssConfig config) {
        return config.createCosClient();
    }

    @Bean(name = "tencentCloudOssService")
    public IOssService tencentCloudOssService(TencentCloudOssConfig config) {
        return new MinioOssService(config);
    }

}
